

using System;
using System.Data;
using System.Diagnostics;
using System.Xml;
using System.ComponentModel;
using System.Collections;
using System.IO;


using CStock;
using CUtil;
using CIndicators;



//problem
//the path var.is not set after deserialization !

//TODO
//Value_Total is not reflecting the current value of the portfolio (Because it would mean an update - and reading 
//of all share data at start
//to update - call Value_TotalUpdate
namespace CStock
{
	[System.Xml.Serialization.XmlRoot()]
	public abstract class Factory : IFactory
    {
		#region fields

        protected IStockObjectBase stockObjectBase;
		

		#endregion



        public Factory()
		{
           //empty - has to be overridden
            
        }

        public IStockObjectBase StockObjectBase
        {
            get
            {
                return this.stockObjectBase;
            }
            set
            {
                stockObjectBase = value;
            }
        }

        public virtual bool Create_Default()
        {
           

            PasswordGenerator defaultPwdGen = new PasswordGenerator(false);
            this.stockObjectBase.ID = defaultPwdGen.Generate();
            return true;
        }
      
        public virtual bool Create_TestData()
        {
            bool b1 = false;
            bool b2 = false;

            Debug.Assert(this.stockObjectBase.ID != null, "Please set the ID of the object " + this.stockObjectBase.GetType().ToString()  + " first");
            if (this.stockObjectBase.ID != null)
            {
                b1 = true;
                Debug.Assert(this.stockObjectBase.ID != string.Empty, "Please set the ID of the object " + this.stockObjectBase.GetType().ToString() + " first");


                if (this.stockObjectBase.ID != string.Empty)
                    b2 = true;
            }
            if(b1 && b2)
                return true;
            return false;

        }
        public virtual bool Create(string id)
        {
            this.stockObjectBase.ID = id;
            
            return true;

        }
       
        public virtual bool Serialize()
        {

            return this.stockObjectBase.Serialize();


        }


        public virtual bool RemoveAll()
        {
            //return XMLSerializable.RemoveAllXML(this.stockObjectBase.Path);
            return XMLSerializable.RemoveAllData(this.stockObjectBase.Path);


        }
        public virtual bool RemoveAllXML()
        {
            return XMLSerializable.RemoveAllXML(this.stockObjectBase.Path);
            //return XMLSerializable.RemoveAllData(this.stockObjectBase.Path);


        }



        /// <summary>
        /// attention - new object variable for dataClass!!
        /// </summary>
        /// <returns></returns>
        public virtual bool DeSerialize()
        {

            object o = this.stockObjectBase;
            if (this.stockObjectBase.DeSerialize(ref o))
            {
                if (o != null)
                    this.stockObjectBase = o as StockObjectBase;
                return true;
            }
            return false;


       

        }
        
       

     
	}
}
